package main

import (
	"fmt"
	"time"

	//"github.com/robinson/gos7"

	modbus "github.com/thinkgos/gomodbus/v2"
)

func main() {
	fmt.Println("Hello World")
	//const (
	//	ipAddr = "169.254.116.76" //PLC IP
	//	rack   = 0                // PLC机架号
	//	slot   = 1                // PLC插槽号
	//)
	////PLC tcp连接客户端
	//handler := gos7.NewTCPClientHandler(ipAddr, rack, slot)
	////连接及读取超时
	//handler.Timeout = 200 * time.Second
	////关闭连接超时
	//handler.IdleTimeout = 200 * time.Second
	////打开连接
	//handler.Connect()
	////函数退出时关闭连接
	//defer handler.Close()
	//
	////获取PLC对象
	//client := gos7.NewClient(handler)
	//
	////输出PLC运行状态
	//fmt.Println(client.PLCGetStatus())
	p := modbus.NewTCPClientProvider("192.168.0.10:502", modbus.WithEnableLogger())
	client := modbus.NewClient(p)
	err := client.Connect()
	if err != nil {
		fmt.Println("connect failed, ", err)
		return
	}
	defer client.Close()

	fmt.Println("starting")
	for {
		_, err := client.ReadCoils(1, 0, 1)
		if err != nil {
			fmt.Println(err.Error() + "---------------------------")
		}

		//	fmt.Printf("ReadDiscreteInputs %#v\r\n", results)

		time.Sleep(time.Second * 2)
	}
}
